//
//  YKDetailSecondViewController.m
//  yuke
//
//  Created by Dejun Liu on 15/1/9.
//  Copyright (c) 2015年 juwan. All rights reserved.
//

#import "YKDetailSecondViewController.h"
#import "DetailHeaderView.h"
#import "YKCountryPlace.h"
#import "YKPlace.h"
#import "YKDestinationCollectionViewCell.h"
#import "YKActivityViewController.h"
#import "YKLineViewController.h"
#import "YKClassicRecomandViewController.h"
#import "YKTravelHelpViewController.h"
#import "YKLandmarkCollectionViewCell.h"

@interface YKDetailSecondViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *tData;

@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) NSArray *cData;

@property (nonatomic,strong) YKLandmark *recomandLandmark; //最上面显示的景点

@end


@implementation YKDetailSecondViewController

#pragma mark - LifeCycle -

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(150, 150);
    layout.minimumInteritemSpacing = 8;
    layout.minimumLineSpacing = 8;
    layout.sectionInset = UIEdgeInsetsMake(10, 6, 10, 6);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 178.0f) collectionViewLayout:layout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = UIColorFromRGB(250, 245, 233);
    [self.collectionView registerNib:[UINib nibWithNibName:@"YKLandmarkCollectionViewCell" bundle:nil]
          forCellWithReuseIdentifier:@"Cell"];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = YKCOLOR_MAIN_YELLOW;
    [self.view insertSubview:self.tableView atIndex:0];
    
    DetailHeaderView *headerView = [DetailHeaderView headerViewWithCGSize:CGSizeMake(_tableView.width, 166.0f)];
    headerView.headerTitleLabel.text = @"";
    headerView.headerImage = [UIImage imageNamed:@"demo2"];
    [headerView.imageScrollView bringSubviewToFront:headerView.camera];
    [headerView btnClickBlock:^(NSString *urlString) {
        
    }];
    self.tableView.tableHeaderView = headerView;
    
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

#pragma mark - Request Data -

- (void)requestData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:@{@"module":@"destinationrecom"}];
    [params hs_setSafeValue:self.curPlace.cateid forKey:@"area"];
    [params hs_setSafeValue:self.curPlace.subcateid forKey:@"city"];
    [[JWWebService sharedInstance]getWithParams:params jsonResponse:^(id responseData, NSError *error) {
        if (responseData) {
            //主推荐景点
            {
                DetailHeaderView *dhv = (DetailHeaderView *)self.tableView.tableHeaderView;
                NSArray *hostPlaceArray = [[responseData objectForKey:@"list"] objectForKey:@"graphicrecom"];
                if (hostPlaceArray.count) {
                    self.recomandLandmark = (YKLandmark *)[[YKLandmark listFromArrayDic:hostPlaceArray] objectAtIndex:0];
                    [dhv.imageView sd_setImageWithURL:[NSURL URLWithString:self.recomandLandmark.LMPic]];
                    self.placeTitle.text = self.recomandLandmark.LMName;
                    self.placeDes.text = self.recomandLandmark.LMDes;
                }else
                {
                    self.placeTitle.text = @"暂无数据！";
                    self.placeDes.text = @"暂无数据！";
                }
                
            }
            
            //下方推荐
            {
                self.cData = [YKLandmark listFromArrayDic:[[responseData objectForKey:@"list"] objectForKey:@"photorecom"]];
                if (_cData.count) {
                    [self.collectionView reloadData];
                }
            }
        }
    }];
}

#pragma mark -
#pragma mark UISCrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == self.tableView)
    {
        // pass the current offset of the UITableView so that the ParallaxHeaderView layouts the subViews.
        [(ParallaxHeaderView *)self.tableView.tableHeaderView layoutHeaderViewForScrollViewOffset:scrollView.contentOffset];
    }
}

#pragma mark -
#pragma mark tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            return self.brief.height;
            break;
        case 1:
            return self.menu.height;
            break;
        case 2:
            return self.collectionView.height;
            break;
        default:
            break;
    }
    return 120.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = YKCOLOR_MAIN_YELLOW;
    }
    [cell.contentView removeAllSubviews];
    switch (indexPath.row) {
        case 0:
        {
            [cell.contentView addSubview:self.brief];
        }
            break;
        case 1:
        {
            [cell.contentView addSubview:self.menu];
        }
            break;
        case 2:
        {
            [cell.contentView addSubview:self.collectionView];
        }
            
        default:
            break;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DLog(@"did selected");
}

#pragma mark -
#pragma mark Collection

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _cData.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YKLandmark *landmark = [self.cData objectAtIndex:indexPath.row];
    
    YKLandmarkCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:landmark.LMPic]];
    cell.ttLabel.text = landmark.LMName;
    cell.commentsNumLabel.text = [NSString stringWithFormat:@"%@条评论",landmark.LMCommentNum];
    [cell.starView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button = obj;
        if (idx+1 <= [landmark.LMStar intValue]) {
            [button setImage:[UIImage imageNamed:@"star1"] forState:0];
        }else
        {
            [button setImage:[UIImage imageNamed:@"star0"] forState:0];
        }
    }];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YKDetailSecondViewController *vc = [[YKDetailSecondViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
#pragma mark 其它
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)favClick:(id)sender {
}

- (IBAction)activity:(id)sender {
    YKActivityViewController *vc = [[YKActivityViewController alloc] init];
    vc.curPlace = self.curPlace;
    vc.type = VCTypeAllActivity;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)lineClick:(id)sender {
    YKActivityViewController *vc = [[YKActivityViewController alloc] init];
    vc.curPlace = self.curPlace;
    vc.type = VCTypeClassicalActivity;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)recomandClick:(id)sender {
    YKClassicRecomandViewController *vc = [[YKClassicRecomandViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)travelHelpClick:(id)sender {
    YKTravelHelpViewController *vc = [[YKTravelHelpViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
